package cn.yunhe.crm.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunhe.crm.dao.ContractMapper;

import cn.yunhe.crm.entity.Contract;
import cn.yunhe.crm.entity.ContractExample;

/**
 * 
 * @author 尹明泉
 *	合同管理
 */
@Service("ContractService")
public class ContractService {
	/**
	 * 自动装配合同dao
	 */
	@Autowired
	private ContractMapper contractdao;

	public void setContractdao(ContractMapper contractdao) {
		this.contractdao = contractdao;
	}
	/**
	 * 
	 * @param id 页面的合同ID
	 * @return list 合同集合，并且关联着机构集合和联系人集合
	 */
	public List<Contract> contractlist(Integer id) {
		Map<String, Integer> map=new HashMap();
		map.put("id", id);
		List<Contract> list=contractdao.contractlist(map);
		return list;
	}

	/**
	 * 查询 合同总数
	 * @return
	 */
	public int checkCount(){
		return contractdao.countByExample(null);
	}
	/**
	 * 根据时间查询合同总数
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	public int checkCountByYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return contractdao.checkCountByYearAndMonth(map);
	}
	/**
	 * 根据合同名称查询此合同是否存在，如果存在返回true，否则返回false
	 * @param string
	 * @return
	 */
	public Contract getContractByMap(Map map) {
		ContractExample exa = new ContractExample();
		ContractExample.Criteria cri = exa.createCriteria();
		Contract c = null;
		if(map.get("conditionname")!=null&&!"".equals(map.get("conditionname"))){
			cri.andNameEqualTo(map.get("conditionname").toString());
		}
		if(map.get("conditionid")!=null){
			cri.andIdEqualTo(new Integer(map.get("conditionid").toString()));
		}
		int r = contractdao.countByExample(exa);
		if(r>0){
			c = contractdao.getAllMessageByMap(map);
		}
		return c;
	}

	/**
	 * 保存合同，并得到合同的id
	 * @param contract 
	 * @return i 
	 */
	public int addcontract(Contract contract){
		contractdao.insert(contract);
		return contract.getId();
	}
	/**
	 * 修改合同
	 * @param contract
	 * @return
	 */
	public int updatecontract(Contract contract) {
		int i=0;
		i=contractdao.updateByPrimaryKeySelective(contract);
		return i;
	}
	public int delcontract(Contract contract) {
		ContractExample example=new ContractExample();
		example.createCriteria().andIdEqualTo(contract.getId());
		//修改删除状态为：删除
		contract.setDelstate(1);
		int i=0;
		i=contractdao.updateByExampleSelective(contract, example);
		return i;
	}
	/**
	 * 查询产品A总数
	 * @return
	 */
	public int checkProductA(){
		return contractdao.checkProductA();
	}
	/**
	 * 查询产品B总数
	 * @return
	 */
	public int checkProductB(){
		return contractdao.checkProductB();
	}
	/**
	 * 查询产品C总数
	 * @return
	 */
	public int checkProductC(){
		return contractdao.checkProductC();
	}
	/**
	 * 通过时间查询产品A总数
	 * @return
	 */
	public int checkProductAbyYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return contractdao.checkProductAbyYearAndMonth(map);
	}
	/**
	 * 通过时间查询产品B总数
	 * @return
	 */
	public int checkProductBbyYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return contractdao.checkProductBbyYearAndMonth(map);
	}
	/**
	 * 通过时间查询产品C总数
	 * @return
	 */
	public int checkProductCbyYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return contractdao.checkProductCbyYearAndMonth(map);
	}
	/**
	 * 查询合同总金额
	 * @return
	 */
	public int checkContractAmount(){
		return contractdao.checkContractAmount();
	}
	/**
	 * 根据时间查询合同总金额
	 * @return
	 */
	public int checkContractAmountByYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return contractdao.checkContractAmountByYearAndMonth(map);
	}
	/**
	 * 通过机构ID查询合同总金额
	 * @param oid
	 * @return
	 */
	public int checkAmountByOid(int oid){
		return contractdao.checkAmountByOid(oid);

	}
	
	public int countByToday(){
		return contractdao.checkCountByToday();
	}
	public int checkCountAproductByToday(){
   	 
   	 return contractdao.checkCountAproductByToday();
    }
    public int checkCountBproductByToday(){
   	 
   	 return contractdao.checkCountBproductByToday();
    }
    public int checkCountCproductByToday(){
   	 
   	 return contractdao.checkCountCproductByToday();
    }
}
